home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / timer.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-04  |  4.5 KB  |  227 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. #ifndef _TIMER_H_
  22. #define _TIMER_H_
  23.  
  24. #ifdef __SVR4 // Solaris
  25. #include <sys/time.h>
  26. #include <strstream.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <sys/procfs.h>
  30. #include <stdio.h>
  31. #endif // __SVR4
  32.  
  33. #include <time.h>
  34.  
  35. #if defined(unix) || defined(__linux)
  36. #include <sys/time.h>
  37. #include <unistd.h>
  38. #endif
  39.  
  40.  
  41. #ifdef __sgi
  42. #include <sys/types.h>
  43. #include <sys/times.h>
  44. #include <limits.h>
  45. #endif
  46.  
  47.  
  48. #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32)
  49. #ifndef WIN32
  50. #define WIN32 1
  51. #endif
  52. #include <windows.h>
  53. #endif
  54.  
  55. #if defined(__BEOS__)
  56. #include <OS.h>
  57. #endif
  58.  
  59. /*** USAGE: ***
  60.  
  61.    Timer t;
  62.  
  63.    t.start();
  64.    // do hairy computation 1.
  65.    t.stop();
  66.  
  67.    cout << "The first hairy computation took " << (double) t << " seconds." << endl;
  68.  
  69.    t.reset();
  70.  
  71.    t.start();
  72.    // do hairy computation 2.
  73.    t.stop();
  74.  
  75.    cout << "The second hairy computation took " << (double) t << " seconds." << endl;
  76.  
  77. */
  78.  
  79. class Timer {
  80.  
  81. public:
  82.  
  83.   Timer (void)
  84.     : _starttime (0),
  85.       _elapsedtime (0)
  86.     {}
  87.  
  88.   // Start the timer.
  89.   void start (void) { _starttime = _time(); }
  90.  
  91.   // Stop the timer.
  92.   void stop (void) { _elapsedtime += _time() - _starttime; }
  93.  
  94.   // Reset the timer.
  95.   void reset (void) { _starttime = _elapsedtime = 0; }
  96.  
  97.   // Set the timer.
  98.   void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);}
  99.  
  100.   // Return the number of seconds elapsed.
  101.   operator double (void) { return _timetosec (_elapsedtime); }
  102.  
  103.  
  104. private:
  105.  
  106.   // The _timer variable will be different depending on the OS.
  107.   // We try to use the best timer available.
  108.  
  109. #ifdef __sgi
  110. #define TIMER_FOUND
  111.  
  112.   long _starttime, _elapsedtime;
  113.  
  114.   long _time (void) {
  115.     struct tms t;
  116.     long ticks = times (&t);
  117.     return ticks;
  118.   }
  119.  
  120.   double _timetosec (long t) {
  121.     return ((double) (t) / CLK_TCK);
  122.   }
  123.  
  124.   long _sectotime (double sec) {
  125.     return (long) sec * CLK_TCK;
  126.   }
  127. #endif
  128.  
  129. #ifdef __SVR4 // Solaris
  130. #define TIMER_FOUND
  131.   hrtime_t    _starttime, _elapsedtime;
  132.  
  133.   virtual hrtime_t _time (void) {
  134.     return gethrtime();
  135.   }
  136.  
  137.   hrtime_t _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); }
  138.  
  139.   double _timetosec (hrtime_t& t) {
  140.     return ((double) (t) / 1.0e9);
  141.   }
  142. #endif // __SVR4
  143.  
  144. #if defined(MAC) || defined(macintosh)
  145. #define TIMER_FOUND
  146.   double        _starttime, _elapsedtime;
  147.  
  148.   double _time (void) {
  149.     return get_Mac_microseconds();
  150.   }
  151.  
  152.   double _timetosec (hrtime_t& t) {
  153.     return t;
  154.   }
  155. #endif // MAC
  156.  
  157. #ifdef WIN32
  158. #define TIMER_FOUND
  159.   DWORD _starttime, _elapsedtime;
  160.  
  161.   DWORD _time (void) {
  162.     return GetTickCount();
  163.   }
  164.  
  165.   double _timetosec (DWORD& t) {
  166.     return (double) t / 1000.0;
  167.   }
  168.  
  169.   unsigned long _sectotime (double sec) {
  170.         return (unsigned long)(sec * 1000);
  171.     }
  172.  
  173. #endif // WIN32
  174.  
  175.  
  176. #ifdef __BEOS__
  177. #define TIMER_FOUND
  178.   bigtime_t _starttime, _elapsedtime;
  179.   bigtime_t _time(void) {
  180.     return system_time();
  181.   }
  182.   double _timetosec (bigtime_t& t) {
  183.     return (double) t / 1000000.0;
  184.   }
  185.   
  186.   bigtime_t _sectotime (double sec) {
  187.     return (bigtime_t)(sec * 1000000.0);
  188.   }
  189. #endif // __BEOS__
  190.  
  191. #ifndef TIMER_FOUND
  192.  
  193.   long _starttime, _elapsedtime;
  194.  
  195.   long _time (void) {
  196.     struct timeval t;
  197.     struct timezone tz;
  198.     gettimeofday (&t, &tz);
  199.     return t.tv_sec * 1000000 + t.tv_usec;
  200.   }
  201.  
  202.   double _timetosec (long t) {
  203.     return ((double) (t) / 1000000);
  204.   }
  205.  
  206.   long _sectotime (double sec) {
  207.     return (long) sec * 1000000;
  208.   }
  209.  
  210. #endif // TIMER_FOUND
  211.  
  212. #undef TIMER_FOUND
  213.  
  214. };
  215.  
  216.  
  217. #ifdef __SVR4 // Solaris
  218. class VirtualTimer : public Timer {
  219. public:
  220.   hrtime_t _time (void) {
  221.     return gethrvtime();
  222.   }
  223. };  
  224. #endif
  225.  
  226. #endif
  227.